package edu.xlh.travel.controller.user;

import com.alipay.api.AlipayApiException;
import edu.xlh.travel.common.Result;
import edu.xlh.travel.pojo.Orders;
import edu.xlh.travel.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
@RequestMapping("/fore_orders")
public class ForeOrderController {
    @Autowired
    private OrdersService ordersService;

    /*
    * buy
    * */
    @RequestMapping("/buy")
    @ResponseBody
    public Result buy(HttpSession session , Orders orders) {
        return ordersService.buy(session , orders);
    }

    /*
    * search all orders
    * */
    @RequestMapping("/findAllOrders")
    @ResponseBody
    public Result findAllOrders(HttpSession session) {
        return ordersService.findAllOrders(session);
    }

    @RequestMapping("/cancelOrder")
    @ResponseBody
    public Result cancelOrder(String orderId , HttpSession session) {
        return ordersService.cancelOrder(orderId , session);
    }

    /*
    * to orderPay page And callBack orderInfo by orderId
    * */
    @RequestMapping("/toPayOrderPage")
    public String toPayOrderPage(String orderId , HttpSession session , Model model) {
        System.out.println("订单号"+orderId);
        return ordersService.toPayOrderPage(orderId , session , model);
    }

    @PostMapping(value = "/doPayForOrder" , produces = "text/html; charset=UTF-8")
    @ResponseBody
    public String doPayForOrder(@RequestBody HashMap<String,String> orderInfo , HttpSession session) throws AlipayApiException {
        return ordersService.doPayForOrder(orderInfo , session);
    }

    @RequestMapping(value = "/consume" )
    @ResponseBody
    public Result consume(String orderId , HttpSession session) {
        return ordersService.consume(orderId , session);
    }

    /*
     * find by orderId
     * */
    @RequestMapping("/findByOrderId/{orderId}/{emailId}" )
    @ResponseBody
    public Result findByOrderId(@PathVariable("orderId") String orderId , @PathVariable("emailId") String emailId , HttpSession session) {
        return ordersService.findByOrderId(orderId , emailId , session);
    }

}

/*

    @RequestMapping("/t4")
    public ModelAndView t4(ModelAndView modelAndView){
        modelAndView.addObject("age",20);
        modelAndView.addObject("username","黎明");
        modelAndView.setViewName("t2");
        return modelAndView;
    }*/
